#include "AssetDelegate.h"
#include "ModelItems/TreeItem.h"
#include "QuestionAdder.h"
#include "../core/src/container/ResponseAttribute.h"

#include "AttributeEditor.h"
#include "QuestionEditor.h"
#include "ResponseAttributeEditor.h"
#include "ResponseEditor.h"
#include "ResultEditor.h"
#include "ModelItems/ResponseAttributeItem.h"

namespace Xps
{
	AssetDelegate::AssetDelegate(XpsAssets::AssetManager* am, QWidget *parent) : QStyledItemDelegate(parent)
	{
		this->am = am;
	}

	QWidget *AssetDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option , const QModelIndex &index ) const
	{
		TreeItem* item = (TreeItem*) ((QStandardItemModel*)index.model())->itemFromIndex(index);
		
		QWidget* lbl;
		
		switch (item->getType())
		{
		case QUESTION:
			lbl = new QuestionEditor();
			break;
		case ATTRIBUTE:
			lbl = new AttributeEditor();
			break;
		case RESULT:
			lbl = new ResultEditor();
			break;
		case RESPONSE:
			lbl = new ResponseEditor((XpsAssets::Response*) item->asset);
			break;
		case RESPONSE_ATTRIBUTE:
			lbl = new ResponseAttributeEditor();
			break;
		}

		((QWidget*) this->parent())->layout()->addWidget(lbl);
		((QWidget*) this->parent())->show();
		((QWidget*) this->parent())->update();
		return lbl;
	}

	void AssetDelegate::setEditorData(QWidget *e, const QModelIndex &index) const
	{
		Editor* editor = (Editor*) e;
		TreeItem* item = (TreeItem*) ((QStandardItemModel*)index.model())->itemFromIndex(index);
		
		XpsAssets::Response* r = NULL;
		ResponseEditor* reditor = NULL;

		switch (item->getType())
		{
		case RESPONSE:
			r = (XpsAssets::Response*) item->asset;
			reditor = (ResponseEditor*) editor;
			reditor->read(r);
			
			for(int i = 0; i < r->attributes()->count(); i++)
			{
				int raid = r->attributes()->keys().at(i);
				XpsAssets::ResponseAttribute* ra = r->attributes()->value(raid);
				QString attributename = am->getAttribute(raid)->name();
				reditor->ra_editor.at(i)->read(ra);
				reditor->editor_lbl.at(i)->setText(attributename);
			}
			break;

		default:
			editor->read(item->asset);
			break;
		}

		editor->setFocus(Qt::OtherFocusReason);
	}

	void AssetDelegate::setModelData(QWidget *e, QAbstractItemModel *model, const QModelIndex &index) const
	{
		Editor* editor = (Editor*) e;
		TreeItem* item = (TreeItem*) ((QStandardItemModel*)index.model())->itemFromIndex(index);
		
		XpsAssets::Response* r = NULL;
		ResponseEditor* reditor = NULL;

		switch (item->getType())
		{
		case RESPONSE:
			reditor = (ResponseEditor*) editor;
			reditor->write(item->asset);

			r = (XpsAssets::Response*) item->asset;
					
			for(int i = 0; i < r->attributes()->count(); i++)
			{
				int raid = r->attributes()->keys().at(i);
				XpsAssets::ResponseAttribute* ra = r->attributes()->value(raid);
				reditor->ra_editor.at(i)->write(ra);
			}
			break;

		default:
			editor->write(item->asset);
			break;
		}
		
		((QWidget*) this->parent())->hide();
	}

	void AssetDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
	{
		editor->update();
		((QWidget*)editor->parent())->update();
	}
}